package frame.project;

import java.io.File;
import java.util.List;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.pm.ResolveInfo;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.Toast;

public class Edit extends Activity {
	Button editBack, editCrop, editRotate, editBlur, editDone;
	ImageProcess imageProcess = new ImageProcess();
	ImageProduce image = new ImageProduce();
	Bitmap bm;
	ImageView imageView;
	int rotate = 0;
	String path;
	int request_code = 0;
	private Uri outputFileUri;
	
	/**
	 * Opens AlbumGrid class to choose which image to edit
	 * @author Marcel
	 * 
	 */
	public void chooseImage(){
		Intent intent = new Intent(this, AlbumGrid.class);
		intent.putExtra("color", "Choose");
		startActivityForResult(intent, request_code);
	}
	
	/**
	 * Check if there is a value passed from the previous activity, which is calling AlbumGrid class
	 * Value passed from Album Grid class is a image's path string
	 * @author Marcel
	 * 
	 */
	@Override 
	public void onActivityResult(int requestCode, int resultCode, Intent data) {     
		super.onActivityResult(requestCode, resultCode, data); 
		
		Bundle extras = new Bundle();
		
		
		if(requestCode == request_code){
			if (resultCode == RESULT_OK){
				
				path = data.getDataString().toString();
				
				// Decode image based on the path received
				bm = image.decodeBitmap(path, 400, 400);
				
				// Set imageView with the image taken from the path
				imageView.setImageBitmap(bm);
					}
		         }   
		
		if(requestCode == RESULT_OK){
			//get the returned data
			extras = data.getExtras();
			//get the cropped bitmap
			Bitmap thePic = extras.getParcelable("data");
			imageView.setImageBitmap(thePic);
			//retrieve a reference to the ImageView
			//ImageView imageView = (ImageView)findViewById(R.id.picture);
			//display the returned cropped image
			//imageView.setImageBitmap(thePic);
			//File f = new File (outputFileUri.getPath());
			//if (f.exists()) f.delete();
           // Intent inten3 = new Intent();
           // startActivity(inten3);
		}

	}
	
		           
		
	
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	    setContentView(R.layout.edit);
	    
	    
	    
	    //Create an image with bitmap
	    imageView = (ImageView)findViewById(R.id.imageView1);
	    imageView.setScaleType(ScaleType.FIT_CENTER);
	    
	    try {
	    	// Check if there is a path passed from camera activity
	    	Bundle extras = getIntent().getExtras();
	    	path = extras.getString("strValue").toString();
	    	bm = image.decodeBitmap(path, 400, 400);
		    imageView.setImageBitmap(bm);
    	}
        catch(Exception e){
        	// If there is nothing passed
        	chooseImage();
    	}
	    
	    editBack = (Button)findViewById(R.id.editBack);
	    editCrop = (Button)findViewById(R.id.editCrop);
	    editRotate = (Button)findViewById(R.id.editRotate);
	    editBlur = (Button)findViewById(R.id.editBlur);
	    editDone = (Button)findViewById(R.id.editDone);
	    
	    editBack.setOnClickListener(new View.OnClickListener() {
	    	public void onClick(View v) {
	    		finish();
	    	}
	    });
	    
	    
	    editCrop.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				//imageProcess.performCrop();	
				Uri myUri = Uri.parse(path); 
				performCrop(myUri, imageView);
			    //imageView.setImageBitmap(bitmap);
			}
		});
	    
	    
	    editRotate.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				if (rotate == 3) {
					rotate = 0; 
				} else {
					rotate++;
				}
				Bitmap bitmap = imageProcess.performRotate(bm, rotate);			    
				imageView.setImageBitmap(bitmap);	
			}
		});
	    
	}
	
	  public void performCrop(Uri uri, ImageView imageView){
	    	//take care of exceptions
		  Intent intent = new Intent("com.android.camera.action.CROP");
		    intent.setType("image/*");
		    List<ResolveInfo> list = getPackageManager().queryIntentActivities( intent, 0 );
		    int size = list.size();
		    /*
		    if (size == 0) {            
		        Toast.makeText(this, "Can not find image crop app", Toast.LENGTH_SHORT).show();

		        return;
		    } else {*/
		        intent.setData(uri);        
		        intent.putExtra("outputX", 300);
		        intent.putExtra("outputY", 300);
		        intent.putExtra("aspectX", 1);
		        intent.putExtra("aspectY", 1);
		        intent.putExtra("scale", true);
		        intent.putExtra("return-data", true);
		        if (size == 1) {
		            Intent i        = new Intent(intent);
		            ResolveInfo res = list.get(0);

		            i.setComponent( new ComponentName(res.activityInfo.packageName, res.activityInfo.name));

		            startActivityForResult(i, RESULT_OK);
		        } else {

		        }

		    }}
	   // }
	  
	  
